package com.central.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.modules.sys.dao.SysRoleMenuDao;
import com.central.modules.sys.entity.SysRoleMenuEntity;
import com.central.modules.sys.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {


    @Override
    public boolean add(Long roleId, List<Long> menuIds) {
        this.cleanByRoleId(roleId);
        if (!CollectionUtils.isEmpty(menuIds)) {
            this.saveBatch(menuIds.stream().map(menuId -> {
                SysRoleMenuEntity roleMenuEntity = new SysRoleMenuEntity();
                roleMenuEntity.setRoleId(roleId);
                roleMenuEntity.setMenuId(menuId);
                return roleMenuEntity;
            }).collect(Collectors.toSet()));
        }
        return true;
    }

    @Override
    public boolean cleanByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenuEntity::getRoleId, roleId);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean cleanByMenuId(Long menuId) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenuEntity::getMenuId, menuId);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean del(Long roleId, Long menuId) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenuEntity::getMenuId, menuId).eq(SysRoleMenuEntity::getRoleId, roleId);
        return this.remove(queryWrapper);
    }

    @Override
    public List<SysRoleMenuEntity> listAllByRoleId(Long roleId) {
        QueryWrapper<SysRoleMenuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysRoleMenuEntity::getRoleId, roleId);
        return this.list(queryWrapper);
    }
}
